package com.project.finance.role.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.finance.common.mapper.TbSysRoleMapper;
import com.project.finance.common.po.TbSysRole;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TbSysRoleMapper tbSysRoleMapper;

    @Override
    public List<TbSysRole> selectAll() {
            List<TbSysRole> tbSysRoles=tbSysRoleMapper.selectAll();
        return tbSysRoles;
    }

    @Override
    public int insert(TbSysRole tbSysRole) {

        return tbSysRoleMapper.insert(tbSysRole);
    }

    @Override
    public int update(TbSysRole tbSysRole) {
        return tbSysRoleMapper.updateByPrimaryKey(tbSysRole);
    }

    @Override
    public int delete(int roleId) {
        return tbSysRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public ResultMsg listRoleByPage(int pageSize, int pageNum) {
        ResultMsg resultMsg = new ResultMsg();

        PageHelper.startPage(pageNum,pageSize);

        List<TbSysRole> tbSysRoles = tbSysRoleMapper.selectAll();

        //获取结果集的分页信息
        PageInfo pageInfo = new PageInfo(tbSysRoles);
        //获取总记录数
        long total = pageInfo.getTotal();
        resultMsg.setTatol(total);
        resultMsg.setData(tbSysRoles);
        return resultMsg;
    }

    //根据账号来查询用户信息
    @Override
    public List<TbSysRole> getSysRole(String rolename) {
        Example example = new Example(TbSysRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("roleName","%"+rolename+"%");
        List<TbSysRole> tbSysRoles=tbSysRoleMapper.selectByExample(example);
        return tbSysRoles;
    }
}
